/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package databook.controller;

import javax.swing.JCheckBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/**
 *
 * @author zach
 */
public class FilterCheckBox extends JCheckBox implements PropertyListener, ItemListener {
    private String key;
    private String prop;
    
    public FilterCheckBox(String text,String key, String prop) {
        super();
        setText(text);
        init(key,prop);
    }
    
    public void init(String key, String prop) {
        this.key = key;
        this.prop = prop;
        setSelected(Props.filter(key,prop));
        Props.addFilterListener(key,prop,this);
        addItemListener(this);
    }
        
    public void propertyChanged(PropertyEvent e) {
        boolean val = ((Boolean)e.value).booleanValue();
        if (this.isSelected() != val) {
            this.setSelected(val);
        }
    }
    
    public void itemStateChanged(ItemEvent evt) {
        if (Props.filter(key,prop) != isSelected()) 
            Props.setFilter(key,prop,isSelected());
    }
}
